#include "Bullet.h"
#include "utils.hpp"

void Bullet::update(float elapsedTime, IMap const & map)
{
    _lifespan -= elapsedTime;
    if (_lifespan < FLOAT_LIMIT || _collide != GameObject::NONE)
    {
        _active = false;
        _gc.freeGC();
        return;
    }

    // Slowing down
    _vel.x = _vel.x - Bullet::SLOWCOEFF * (_dir == GameObject::LEFT ? -1 : 1);

    GameObject::update(elapsedTime, map);
}

void Bullet::checkCollision(GameObject & oth, float const elapsedTime)
{
    if (&oth == _shooter)
        return;
    GameObject::checkCollision(oth, elapsedTime);
}

void Bullet::onHit() { _active = false; }

void Bullet::init(GameObject const * shooter)
{
    _active = true;
    _dir = shooter->getDir();
    _pos = shooter->getPosition() + shooter->getSize() / 2 - _size / 2;
    _vel = shooter->getVelocity();
    _vel.x = _vel.x + Bullet::SPEEDADD * (_dir == GameObject::LEFT ? -1 : 1);
    _vel.y = 0;
    _size.set(Bullet::WIDTH, Bullet::HEIGHT);
    _collide = GameObject::NONE;
    _lifespan = Bullet::LIFESPAN;
    _shooter = shooter;
    setGraphicComponent("fireball");
    _gc.flip(_dir == GameObject::LEFT);
}
